package main

import (
	"context"
	pb "github-trpc-polaris-demo/proto" // 使用您的proto包
	"trpc.group/trpc-go/trpc-go/client"
	"trpc.group/trpc-go/trpc-go/log"
	_ "trpc.group/trpc-go/trpc-naming-polarismesh" // 关键：命名中心插件
	naming "trpc.group/trpc-go/trpc-naming-polarismesh"
)

func init() {
	registerService()
}

// 可以通过环境变量或配置文件注入实际值
func registerService() {
	addrs := "172.30.63.45:8091"
	dft := true
	if err := naming.SetupWithConfig(&naming.Config{
		Name:        "polarismesh",
		AddressList: addrs,
		Default:     &dft,
		Protocol:    "grpc",
	}); err != nil {
		// Handle error
	}
}

func main() {
	opts := []client.Option{
		client.WithNamespace("Development"),
		client.WithServiceName("trpc.helloworld.greeter.Greeter"),
	}

	//opts := []client.Option{
	//	client.WithNamespace("Development"),
	//	client.WithTarget("polarismesh://trpc.helloworld.greeter.Greeter"),
	//}

	clientProxy := pb.NewGreeterClientProxy(opts...)
	req := &pb.HelloRequest{
		// 根据您的proto定义填写请求字段
		Name: "Eric",
	}

	ctx := context.Background()
	rsp, err := clientProxy.SayHello(ctx, req)
	if err != nil {
		log.Error("调用失败: %v", err)
	}

	log.Info("调用成功 - 请求: %v, 响应: %v", req, rsp)
}
